package com.bwie.spike.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.spike.dao.GoodsDao;
import com.bwie.spike.entity.GoodsEntity;
import com.bwie.spike.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 页面缓存化处理
     * */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {



        String goodlist = redisTemplate.opsForValue().get("goodlist");

        if (StringUtils.isNotBlank(goodlist)){
            PageUtils pageUtils = JSON.parseObject(goodlist, new TypeReference<PageUtils>() {
            });
            return pageUtils;

        }else {
            IPage<GoodsEntity> page = this.page(
                    new Query<GoodsEntity>().getPage(params),
                    new QueryWrapper<GoodsEntity>()
            );
            PageUtils pageUtils = new PageUtils(page);
            redisTemplate.opsForValue().set("goodlist", JSON.toJSONString(pageUtils));
            return pageUtils;
        }


    }

}